import QtQuick 1.0
//import QtWebKit 1.0
import "../Components/"
import "../WebAccess/QMLTwitter/"

//Auth認証のダイアログ
DialogBase {
    id: _root
    width: calcWidth()
    height: calcHeight(_root)
    x: 0
    y: 0

    dialogTitle: "Authorization"

    signal addPressed       // 閉じる
    signal closePressed    // 閉じるイベント

    property alias twitter: _twitter

    function viewEvent(){
        // clear
        _twitter.oauthToken = "";
        _twitter.oauthTokenSecret = "";
        _twitter.screenName = "";
        _twitter.userId = "";

        // set
        _inputPIN.text = "";
        _authInfo.text = "Authorized : " + _twitter.screenName;
        _root.forceActiveFocus();
    }

    // 幅計算
    function calcWidth(){
        if(false){//_Device.isMobile){
            return parent.width;
        }else{
            return parent.referenceWidth * 0.9;
        }
    }

    // 高さ計算
    function calcHeight(obj){
        if(false){// _Device.isMobile){
            return parent.height
        }else{
            var max_height = _items.height + titleHeight + 40;
    //        for(var i=0; i<obj.children.length; i++){
    //            if(max_height < obj.children[i].height){
    //                max_height = obj.children[i].height;
    //            }
    //        }
            if(max_height > _root.parent.height){
                max_height = _root.parent.height - _root.border.width * 2;
            }
            return max_height;
        }
    }

    QMLTwitterAuth{
        id: _twitter
        consumerSecret: _twitterSetting.consumerSecret
        consumerKey: _twitterSetting.consumerKey
        screenName: "" //_twitterSetting.screenName
        userId: "" // _twitterSetting.userId

        property string authUrl: "" // pin取得して認証に行くURL

        //1 PIN取得
        onResponseRequestToken: {
            authUrl = url;
            Qt.openUrlExternally(url);
//                _pinWebView.url = url;
//                _webViewFlickable.height = 200;
//                _loaderContent.sourceComponent = _webViewComponet;
        }
        //2
        onResponseAccessToken: {
            if(token.length === 0 || token_secret === 0){
                //ng
                _toast.show("Fail authorization");
            }else{
                //ok
                oauthToken = token;
                oauthTokenSecret = token_secret;
                screenName = screen_name;
                userId = user_id;

                // view
                _toast.show("Success authorization!");
                _authInfo.text = "Authorized : " + screen_name;
            }

        }
    }

    Keys.onEscapePressed: {
        closePressed();
    }

    Column {
        id: _items
        width: _root.width * 0.9
        anchors.top: _root.titleBottom
        anchors.topMargin: 15
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 15

        Button {
            id: _buttonStartOAuth
            width: parent.width
//            height: 25
            text: "1.Get PIN Code"
            font.pixelSize: _Setting.fontPixelSizeButton * _root.density

            onClicked : {
                //oAuth認証開始 PIN取得までしてもらう
                _twitter.requestToken();
            }
        }

        // 認証用のWebView
//        Flickable{
//            id: _webViewFlickable
//            width: parent.width
//            height: 0 // parent.width
//            contentWidth: _pinWebView.width
//            contentHeight: _pinWebView.height
//            clip: true
//            WebView{
//                id: _pinWebView
//                preferredWidth: parent.width
////                width: parent.width
//                url: ""// _buttonStartOAuth.authUrl
//            }
//        }

        Row{
            id: _inputArea
            width: parent.width
            height: _inputPIN.height
            spacing: 5
            Text {
                id: _inputLabel
                height: parent.height
                verticalAlignment: Text.AlignVCenter
                font.pixelSize: _Setting.fontPixelSizeDlgMessageS * _root.density
                text: "2.PIN ->"
                color: "#ffffff"
            }
            TextBox {
                id: _inputPIN
                width: parent.width - _inputLabel.width
                fontPixelSize: _Setting.fontPixelSizeInputBox * _root.density
                anchors.top: parent.top
            }
        }


        //認証ボタン
        Button {
            id: _buttonOAuth
            width: parent.width
            text: "3.Authorization"
            font.pixelSize: _Setting.fontPixelSizeButton * _root.density

            onClicked : {
                //PINで認証する
                _twitter.accessToken(_inputPIN.text);
            }
        }

        //認証情報の表示
        Text {
            id: _authInfo
            height: font.pixelSize
            font.pixelSize: _Setting.fontPixelSizeDlgMessageS * _root.density
            text: "Authorized : " + _twitter.screenName
            color: "#ffffff"
        }


        Row{
            spacing: 15

            //OK
            Button {
                id: _buttonClear
                text: "    Add    "
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density
                onClicked : {
                    if(_twitter.oauthToken.length > 0 && _twitter.oauthTokenSecret.length > 0){
                        _root.addPressed();
                    }else{
                        _toast.show("fail");
                    }

                }
            }

            //閉じる
            Button {
                id: _buttonClose
                text: " Cancel "
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density
                onClicked : {
                    _root.closePressed();
                }
            }
        }
    }

}
